-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Refactor InstNamer::GetScopeIdOffset()
to make it easier to comprehend and maintain
#6159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…end and maintain Instead of having a switch case with `fallthrough` where each case adds the number of entities of the next id type case, have an array that maps id type to a function that gets the number of the matching entities. This array is less confusing and easier to maintain to avoid bugs like the one fixed in carbon-language#6151. The logic that sums only the entities above the given id is now a common logic so no need to change it when changing the set of ids.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels pretty subjective... for me personally, this looks worse.
To clarify my comment, before, the code block was:
Now it looks like the code block is:
Both of these seem similar to me, with a couple differences; I always find lambdas add complexity for me, but also the new code has more general framework (i.e. more to understand). So where you say: "The logic that sums only the entities above the given id is now a common logic so no need to change it when changing the set of ids." I don't see how the two are much different: there's a code block for a given group, and additions need an addition. I completely understand that you may find it easier to reason about, but to me the addition of a struct and complex control flow is not fundamentally easier to reason about. If you want something simpler, I'd suggest a solution that drops both the struct and lambdas. |
offset += sem_ir_->cpp_overload_sets().size(); | ||
[[fallthrough]]; | ||
case ScopeIdTypeEnum::For<CppOverloadSetId>: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if we grouped these things with whitespace so that you saw the things above the case
are related to it more clearly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Created #6165.
Instead of having a switch case with
fallthrough
where each case adds the number of entities of the next id type case, have an array that maps id type to a function that gets the number of the matching entities.This array is less confusing and easier to maintain to avoid bugs like the one fixed in #6151.
The logic that sums only the entities above the given id is now a common logic so no need to change it when changing the set of ids.